Chapter 11 — Creating a Dates Dimension Table
Code Reference File — Copy and paste as needed

========================================
11.2.1 Create Date Range
========================================
sales['Date'] = pd.to_datetime(sales['Date'])
min_date = sales['Date'].min()
max_date = sales['Date'].max()
dates = pd.DataFrame({'Date': pd.date_range(start=min_date, end=max_date)})
print(dates.head())

========================================
11.2.2 Add Date Attributes
========================================
dates['Year']        = dates['Date'].dt.year
dates['Month']       = dates['Date'].dt.month
dates['Month Name']  = dates['Date'].dt.strftime('%B')
dates['Quarter']     = dates['Date'].dt.quarter
dates['Day']         = dates['Date'].dt.day
dates['Day of Week'] = dates['Date'].dt.dayofweek
dates['Day Name']    = dates['Date'].dt.strftime('%A')
dates['Week Number'] = dates['Date'].dt.isocalendar().week
print(dates.head())

========================================
11.2.3 Financial Year and Month
========================================
dates['Financial Year']  = dates['Date'].apply(lambda x: x.year + 1 if x.month >= 7 else x.year)
dates['Financial Month'] = dates['Date'].apply(lambda x: (x.month - 6) if x.month >= 7 else (x.month + 6))
print(dates[['Date', 'Financial Year', 'Financial Month']].head(15))

========================================
11.3 Join Dates to Sales
========================================
sales_with_dates = sales.merge(dates, left_on='Date', right_on='Date', how='left')
print(sales_with_dates.head())

========================================
11.4 Revenue by Financial Year
========================================
revenue_by_fy = sales_with_dates.groupby('Financial Year')['Revenue'].sum()
print(revenue_by_fy)
